﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLite;

namespace MOJsdk
{
    public class MojMovie : MoJBasicMovie, LogicalClass
    {
        private MojMovieFileCollection _movies;
        private MojImageFileCollection _images;
        private MojSubtitleFileCollection _sub;

        #region Inicialization
        public MojMovie(MoJBasicMovie BaseMovie)
        {
            SetBasicMovie(BaseMovie);
        }
        public MojMovie(string MovieCode)
        {
            MojSystem system = MojHost.GetHost.MoJSystem;
            if (system.MoviesInterop.MovieExist(MovieCode))
                SetBasicMovie(system.MoviesInterop.GetMovie(MovieCode));
            else
                this.MovieCode = MovieCode;
        }

        private void SetBasicMovie(MoJBasicMovie BaseMovie)
        {
            MovieCode = BaseMovie.MovieCode;
            MovieCast = BaseMovie.MovieCast;
            MovieDirectors = BaseMovie.MovieDirectors;
            MovieGenres = BaseMovie.MovieGenres;
            MovieOriginalTitle = BaseMovie.MovieOriginalTitle;
            MovieTagline = BaseMovie.MovieTagline;
            MovieTitle = BaseMovie.MovieTitle;
            MovieWriters = BaseMovie.MovieWriters;
            MovieYear = BaseMovie.MovieYear;
            Imdb = BaseMovie.Imdb;

            INICollections();
        }
        private void INICollections()
        {
            _movies = new MojMovieFileCollection(this.MovieCode);
            _images = new MojImageFileCollection(this.MovieCode);
            _sub = new MojSubtitleFileCollection(this.MovieCode);
        }
        #endregion

        #region Properties
        public MojMovieFileCollection MovieFiles
        { get { return _movies; } }
        public MojImageFileCollection ImageFiles
        { get { return _images; } }
        public MojSubtitleFileCollection SubtitleFiles
        { get { return _sub; } }

        public string CoverPath
        {
            get
            {
                if (ImageFiles.CoverImages.Count < 1)
                    return MojEnviromentVarsEnum.DefaultCoverPath.Value();

                MojImageFile file = ImageFiles.CoverImages[0];
                if (file.IsAvaible)
                    return file.FileFullPath;

                return MojEnviromentVarsEnum.DefaultCoverPath.Value();
            }
        }
        public string BackdropPath
        {
            get
            {
                if (ImageFiles.BackDropImages.Count < 1)
                    return MojEnviromentVarsEnum.DefaultCoverPath.Value();

                MojImageFile file = ImageFiles.BackDropImages[0];
                if (file.IsAvaible)
                    return file.FileFullPath;

                return MojEnviromentVarsEnum.DefaultCoverPath.Value();
            }
        }
        public bool IsAvaible
        {
            get
            {
                return (ImageFiles.IsAvaible && MovieFiles.IsAvaible && SubtitleFiles.IsAvaible);
            }
        }
        public string Query
        {
            get
            {
                string query = string.Format("{1}{2}{3}{4}{5}{6}{7}{8}{9}{0}{10}{11}",
                    MovieCode, MovieCast, MovieDirectors, MovieGenres, MovieOriginalTitle, MovieTagline, MovieTitle,
                    MovieWriters, MovieYear, SubtitleFiles.AvaibleLanguage.ToString(";"),
                    MovieFiles.Select(z=>z.OriginalName).ToString(";")).ToLower();
                return query;
            }
        }

        public string ImdbUrl
        { get { return "http://www.imdb.com/title/" + this.Imdb; } }
        public string TheMovieDbUrl
        { get { return "http://www.themoviedb.org/movie/" + this.MovieCode; } }
        #endregion

        public bool TestStatment(LogicStatment statment)
        {
            string method = statment.BuildClass("\n");
            return ScriptInterop.RunLogicMethod(method, this);
        }
    }
    public class MoJBasicMovie : IEquatable<MoJBasicMovie>
    {
        public MoJBasicMovie() { }
        public MoJBasicMovie(MojWebMovie m)
        {
            MovieCode = m.MovieId;
            Imdb = m.Imdb_Id;
            MovieTitle = m.Title;
            MovieOriginalTitle = m.OriginalTitle;
            MovieYear = m.Year;
            MovieGenres = m.Genres.ToString(";");
            MovieCast = m.Cast.ToString(";");
            MovieDirectors = m.Director.ToString(";");
            MovieWriters = m.Writer.ToString(";");
            MovieTagline = m.Tagline;
        }

        [PrimaryKey, Unique]
        public string MovieCode { get; set; }

        public string Imdb { get; set; }
        public string MovieTitle
        { get; set; }
        public string MovieOriginalTitle
        { get; set; }
        public string MovieYear
        { get; set; }
        public string MovieGenres
        { get; set; }
        public string MovieCast
        { get; set; }
        public string MovieDirectors
        { get; set; }
        public string MovieWriters
        { get; set; }
        public string MovieTagline
        { get; set; }

        //IEquatable<MoJBasicMovie>
        public bool Equals(MoJBasicMovie m)
        {
            return MovieCode == m.MovieCode;
        }

        //Explicit Converters
        public static explicit operator MoJBasicMovie(string MovieCode)
        { return new MoJBasicMovie() { MovieCode = MovieCode }; }
    }
}
